<?php

defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.controller');

/**
 * Component Item Controller
 */
class TaxiBookingControllerDistricts extends TaxiBookingController
{
	/**
	 * Constructor
	 *
	 * @since 1.0
	 */
	function __construct()
	{
		parent::__construct();

		// Register Extra task
		$this->registerTask( 'add'  ,		 	'edit' );
		$this->registerTask( 'apply', 			'save' );
	}

	/**
	 * Logic to save an item
	 *
	 * @access public
	 * @return void
	 * @since 1.0
	 */
	function save()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$task		= JRequest::getVar('task');

		//Sanitize
		$post = JRequest::get( 'post' );

		$model = $this->getModel('district');

		if ( $model->store($post) ) {

			switch ($task)
			{
				case 'apply' :
					$link = 'index.php?option=com_taxibooking&view=district&cid[]='.(int) $model->get('id');
					break;

				default :
					$link = 'index.php?option=com_taxibooking&view=districts';
					break;
			}
			$msg = JText::_( 'DISTRICT SAVED' );

			$cache = &JFactory::getCache('com_taxibooking');
			$cache->clean();

		} else {
			$msg = JText::_( 'ERROR SAVING ITEM' );
			JError::raiseError( 500, $model->getError() );
			$link 	= 'index.php?option=com_taxibooking&view=district';
		}

		$this->setRedirect($link, $msg);
	}
	
        /**
	 * Logic to publish categories
	 *
	 * @access public
	 * @return void
	 * @since 1.0
	 */
	function publish()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
		
		$cid 	= JRequest::getVar( 'cid', array(0), 'post', 'array' );

		if (!is_array( $cid ) || count( $cid ) < 1) {
			$msg = '';
			JError::raiseWarning(500, JText::_( 'SELECT ITEM PUBLISH' ) );
		} else {

			$model = $this->getModel('districts');

			if(!$model->publish($cid, 1)) {
				JError::raiseError(500, $model->getError());
			}

			$msg 	= JText::_( 'DISTRICT PUBLISHED');
		
			$cache = &JFactory::getCache('com_taxibooking');
			$cache->clean();
		}

		$this->setRedirect( 'index.php?option=com_taxibooking&view=districts', $msg );
	}

	/**
	 * Logic to unpublish categories
	 *
	 * @access public
	 * @return void
	 * @since 1.0
	 */
	function unpublish()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
		
		$cid 	= JRequest::getVar( 'cid', array(0), 'post', 'array' );

		if (!is_array( $cid ) || count( $cid ) < 1) {
			$msg = '';
			JError::raiseWarning(500, JText::_( 'SELECT ITEM UNPUBLISH' ) );
		} else {

			$model = $this->getModel('districts');

			if(!$model->publish($cid, 0)) {
				JError::raiseError(500, $model->getError());
			}

			$msg 	= JText::_( 'DISTRICT UNPUBLISHED');
		
			$cache = &JFactory::getCache('com_taxibooking');
			$cache->clean();
		}
		
		$this->setRedirect( 'index.php?option=com_taxibooking&view=districts', $msg );
	}
        
	/**
	 * Logic to orderup an item
	 *
	 * @access public
	 * @return void
	 * @since 1.0
	 */
	function orderup()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
		
		$model = $this->getModel('districts');
		$model->move(-1);

		$this->setRedirect( 'index.php?option=com_taxibooking&view=districts');
	}

	/**
	 * Logic to orderdown an item
	 *
	 * @access public
	 * @return void
	 * @since 1.0
	 */
	function orderdown()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
		
		$model = $this->getModel('districts');
		$model->move(1);

		$this->setRedirect( 'index.php?option=com_taxibooking&view=districts');
	}

	/**
	 * Logic to mass ordering items
	 *
	 * @access public
	 * @return void
	 * @since 1.0
	 */
	function saveorder()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
		
		$cid 	= JRequest::getVar( 'cid', array(0), 'post', 'array' );
		$order 	= JRequest::getVar( 'order', array(0), 'post', 'array' );

		$model = $this->getModel('districts');
		if(!$model->saveorder($cid, $order)) {
			$msg = '';
			JError::raiseError(500, $model->getError());
		} else {
			$msg = JText::_( 'NEW ORDERING SAVED' );
		}

		$this->setRedirect( 'index.php?option=com_taxibooking&view=districts', $msg );
	}

	/**
	 * Logic to delete items
	 *
	 * @access public
	 * @return void
	 * @since 1.0
	 */
	function remove()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
		
		$cid		= JRequest::getVar( 'cid', array(0), 'post', 'array' );

		if (!is_array( $cid ) || count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'SELECT ITEM DELETE' ) );
		}

		$model = $this->getModel('districts');

		if(!$model->delete($cid)) {
			$msg = '';
			JError::raiseError(500, $model->getError());
		} else {
			$total 	= count( $cid );
			$msg 	= $total.' '.JText::_('DISTRICT DELETED');

			$cache = &JFactory::getCache('com_taxibooking');
			$cache->clean();
		}

		$this->setRedirect( 'index.php?option=com_taxibooking&view=districts', $msg );
	}

	/**
	 * logic for cancel an action
	 *
	 * @access public
	 * @return void
	 * @since 1.0
	 */
	function cancel()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$this->setRedirect( 'index.php?option=com_taxibooking&view=districts' );
	}

	/**
	 * Logic to create the view for the edit categoryscreen
	 *
	 * @access public
	 * @return void
	 * @since 1.0
	 */
	function edit( )
	{		
		JRequest::setVar( 'view', 'district' );
		JRequest::setVar( 'hidemainmenu', 1 );

		parent::display();
	}

}